package com.icesoft.framework.core.util;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;

import java.util.ArrayList;
import java.util.List;

public class DozerUtil {

	private static final Mapper mapper = new DozerBeanMapper();


	public static <D, E> E convert(D t, Class<E> clazz) {
		if (t == null) {
			return null;
		}
		return mapper.map(t, clazz);
	}

	public static <D, E> List<E> convert(D[] ts, Class<E> clazz) {
		if (ts == null) {
			return new ArrayList<>();
		}
		List<E> es = new ArrayList<>();
		for (D d : ts) {
			E e = convert(d, clazz);
			if (e != null)
				es.add(e);
		}

		return es;
	}

	/**
	 * List集合之间转换
	 */
	public static <D, E> List<E> convert(List<D> tl, Class<E> clazz) {
		if (tl == null) {
			return new ArrayList<>();
		}
		List<E> es = new ArrayList<>();
		for (D d : tl) {
			E e = convert(d, clazz);
			if (e != null) {
				es.add(e);
			}
		}
		return es;
	}

}
